//
//  AppDelegate.m
//  PokerPlus
//
//  Created by sunrui-MacBook on 11-7-29.
//  Copyright 2011 139.ME. All rights reserved.
//

#import "AppDelegate.h"

@implementation AppDelegate
@synthesize mainsocket,eventLogin,hostName,port;
@synthesize roomID;
+ (AppDelegate *)sharedDelegate{
	
	return (AppDelegate *)[[UIApplication sharedApplication] delegate];
}

- (UIView *)rootView{
	return nil;
}

-(void)awakeFromNib{
	mainsocket = [[SocketEngine alloc] init];
	eventLogin = [[LoginEventListener alloc] init];
	[eventLogin CreateMap:hMax];
	[mainsocket setEventListener:eventLogin];
	
	
	hostName = @"192.168.0.66";
	port = 8001;
	enterBackground = YES;
	[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
}

- (void)ServerConnected:(UIView *)viewtoremove{
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:1.0f];
	viewtoremove.alpha = 0.0f;
	[UIView commitAnimations];
}
-(void)cmdGetUserInfo:(SocketEngine *)socket{
	NSLog(@"发送用户数据:");
	[[NSUserDefaults standardUserDefaults] setObject:@"aaa" forKey:USERNAME];
	[[NSUserDefaults standardUserDefaults] setObject:@"aaa" forKey:USERPSW];
	NSString * userName = [[NSUserDefaults standardUserDefaults] objectForKey:USERNAME];
	NSString * userPSW;
	if (userName == nil) {
		
		userName = [[UIDevice currentDevice] uniqueIdentifier];
		userPSW = @"guest";
		
	}
	else {
		userPSW = [[NSUserDefaults standardUserDefaults] objectForKey:USERPSW];
	}
	
	[socket NewMessage:hchLogin];
	
	GetUsrInfo ua = NewAccount([userName dataUsingEncoding:NSASCIIStringEncoding], [userPSW dataUsingEncoding:NSASCIIStringEncoding]);
	NSData * uadat = [NSData dataWithBytes:&ua length:sizeof(ua)];
	[socket WriteData:uadat];
	
	[socket Send];
	
}

-(void)cmdGetRoomSort:(SocketEngine *)socket{
	
	[socket NewMessage:hchGetRoomSort];
	[socket WriteNonData:0];
	[socket Send];
}

-(void)cmdGetRoomList:(SocketEngine *)socket withRoomSort:(int)sort{
	
	[socket NewMessage:hchGetRoomList];
	[socket WriteInt:sort];
	[socket Send];
}
-(void)cmdGetRoomInfo:(SocketEngine *)socket withRoomId:(int)roomId{
	[socket NewMessage:hchGetRoomInfo];
	[socket WriteInt:roomId];
	[socket Send];
}


-(void)cmdEnterRoom:(SocketEngine *)socket{
	[socket NewMessage:hchEnterRoom];
	[socket WriteInt:roomID];
	[socket Send];
}


- (void)dealloc {
    [mainsocket close];
	[mainsocket release];
	[eventLogin release];
    [super dealloc];
}



-(void)rsSetUserInfo:(SocketEngine *)socket{}
-(void)rsSetRoomSort:(SocketEngine *)socket{}
-(void)rsSetRoomList:(SocketEngine *)socket{}
-(void)rsSetRoomInfo:(SocketEngine *)socket{}
-(void)rsEnterRoom:(SocketEngine *)socket withResult:(BOOL)result{}
- (void)rsSitDown:(SocketEngine *)socket withResult:(BOOL)result{}
- (void)rsStandUp:(SocketEngine *)socket withResult:(BOOL)result{}
-(void)rsRefreshGameInfo:(SocketEngine *)socket{}

-(NSString *) getCurrentLanguage
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
    
    NSString *currentLanguage = [languages objectAtIndex:0];
    return currentLanguage;
}

-(void)cmdSitDown:(SocketEngine *)socket atIndex:(int)index{
	[socket NewMessage:hchSitDown];
	SitDown sd = NewSitDown(roomID, index, 1000);
	NSData * sddat = [NSData dataWithBytes:&sd length:sizeof(SitDown)];
	[socket WriteData:sddat];
	[socket Send];
}


-(void)cmdStandUp:(SocketEngine *)socket atIndex:(int)index{
	[socket NewMessage:hchStandUp];
	[socket WriteInt:index];
	[socket Send];
}


@end
